CREATE TABLE [dbo].[tb_EspaciosCurriculares]
(
[idEspacioCurricular] [int] NOT NULL IDENTITY(1, 1),
[Descripcion] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Carrera] [int] NOT NULL,
[Curso] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Tipo] [int] NOT NULL,
[Asignatura] [int] NOT NULL,
[Horas] [int] NOT NULL,
[Duracion] [int] NOT NULL,
[idSARRHH_espacio_curricular_id9] [int] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[deletetb_EspaciosCurriculares] ON [dbo].[tb_EspaciosCurriculares]
 FOR DELETE AS 
SET NOCOUNT ON
		 DELETE tb_AsignaturasPosiblesEC FROM tb_AsignaturasPosiblesEC
		 WHERE idEspacioCurricular = (SELECT idEspaciocurricular FROM deleted)
		 --DELETE tb_Correlatividades FROM tb_Correlatividades
		 --WHERE idEspacioCurricular = (SELECT idEspaciocurricular FROM deleted)
		 --DELETE tb_Correlatividades FROM tb_Correlatividades
		 --WHERE idEspacioCurricular2 = (SELECT idEspaciocurricular FROM deleted)

GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_EspaciosCurriculares] ON [dbo].[tb_EspaciosCurriculares]
 FOR UPDATE AS 
SET NOCOUNT ON
 IF (update(idEspaciocurricular)) begin 
	 SELECT * FROM inserted 
	 IF @@ROWCOUNT = 1 begin 
		 UPDATE tb_AsignaturasPosiblesEC
		 SET idEspacioCurricular = (SELECT idEspaciocurricular FROM inserted)
		 WHERE tb_AsignaturasPosiblesEC.idEspacioCurricular = (SELECT idEspaciocurricular FROM deleted)
		-- UPDATE tb_Correlatividades
		 --SET idEspacioCurricular = (SELECT idEspaciocurricular FROM inserted)
		 --WHERE tb_Correlatividades.idEspacioCurricular = (SELECT idEspaciocurricular FROM deleted)
		 --UPDATE tb_Correlatividades
		 --SET idEspacioCurricular2 = (SELECT idEspaciocurricular FROM inserted)
		 --WHERE tb_Correlatividades.idEspacioCurricular2 = (SELECT idEspaciocurricular FROM deleted)
	end  ELSE begin 
		 ROLLBACK TRANSACTION 
 end 
 end

GO
ALTER TABLE [dbo].[tb_EspaciosCurriculares] ADD CONSTRAINT [PK_tb_EspaciosCurriculares] PRIMARY KEY NONCLUSTERED  ([idEspacioCurricular]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_EspaciosCurriculares] WITH NOCHECK ADD CONSTRAINT [FK_tb_EspaciosCurriculares_tb_Carreras] FOREIGN KEY ([Carrera]) REFERENCES [dbo].[tb_Carreras] ([idCarrera])
GO
ALTER TABLE [dbo].[tb_EspaciosCurriculares] NOCHECK CONSTRAINT [FK_tb_EspaciosCurriculares_tb_Carreras]
GO
